package pcsd.test;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import pcsd.server.JobQueueServer;
import pcsd.task.SleepTask;

/**
 * This class starts the server and gives it sleep tasks.
 *
 * @author Benjamin Ertl, Justinas Murzinas, Boyan Mihaylov
 */
public class SleepTestJob {

    /**
     * Program entry point.
     * 
     * @param args The command-line arguments.
     */
	public static void main(String[] args) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		try {
			// Find the server
			String name = "server";
			Registry registry = LocateRegistry.getRegistry();
			JobQueueServer serv = (JobQueueServer) registry.lookup(name);
			
			testThroughput(serv);

		} 
		catch (Exception e) {
			System.err.println("Test exception:");
			e.printStackTrace();
		}
	}
private static void testThroughput(JobQueueServer serv){

		double whole = 0;
		int it = 10000;

try{
		for(int j=0; j<5; j++){

			long startTime = System.currentTimeMillis();
		for (int i = 0; i < it; i++) {


			serv.enqueueTask(new SleepTask(5 + (i%5) * 50 ));


		}
			long stopTime = System.currentTimeMillis();
						double elapsedTime = (stopTime - startTime);

                whole += 1000*it / elapsedTime;
                
                System.out.println(elapsedTime);
				
		Thread.sleep(4000);
	        }
	        
	        System.out.println("Average whole Time for enqueueTask: " + (whole / 5) + "tasks/s");
	        }
	        catch(Exception e){
	        e.printStackTrace();
	        }
	}
}
